define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'html2canvas'], function ($, undefined, Backend, Table, Form, h2c) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'region/alarm/daily/index',
                    add_url: 'region/alarm/daily/add',
                    edit_url: 'region/alarm/daily/edit',
                    del_url: 'region/alarm/daily/del',
                    multi_url: 'region/alarm/daily/multi',
                    table: 'region_alarm_daily',
                    detail_url : 'region/alarm/daily/detail',
                }
            });

            var table = $("#table");
            var alarm_way_arr = {"1":__('邮箱'),"2":__('企业微信'),"3" : __('钉钉'),"9":__('全部')};
            var switch_arr = {"1":__('开'),"0":__('关')};
            var mail_group = Config.group;
            var department_data = Config.department_data;
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'),formatter: function(value,row,index){
                            return row.id;
                        }},
                       {field: 'tag.id', title: __('局点'),searchList:regionList,formatter: function(value,row,index){
                            return row.tag.name;
                        }},
                        /*{field: 'config_id', title: __('Config_id')},*/
                        {field: 'alarm_way', title: __('Alarm_way'),searchList:alarm_way_arr,formatter:function(value,row,index){
                            if(value == 1){
                                return '邮箱';
                            }else if(value == 2){
                                return '企业微信';
                            }else if(value ==3){
                                return '钉钉';
                            }else{
                                return '全部';
                            }                           
                        }},
                        /*{field: 'mail_group_ids', title: __('Mail_group_ids')},
                        {field: 'mail_ids', title: __('Mail_ids')},
                        {field: 'wechat_department_ids', title: __('Wechat_department_ids')},
                        {field: 'wechat_user_ids', title: __('Wechat_user_ids')},*/
                        {field: 'switch', title: __('Switch'),searchList:switch_arr, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                          buttons: [
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]    
                        }
                    ]
                ]
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);

            //截屏按钮
            $(".btn-shot").click(function(event){
                event.preventDefault();  
                html2canvas(document.body, {
                  onrendered: function(canvas) {
                    var imgbase64 =  canvas.toDataURL({format: 'image/png', quality:1});
                    document.body.appendChild(canvas); //在页面显示
                    var start = imgbase64.indexOf('base64,') + 7;
                    imgbase64 = imgbase64.substring(start, imgbase64.length);
                    imgbase64 = imgbase64.replace(/\+/g, "%2B");
                    //保存下来
                    $.ajax({
                       type: "POST",
                       url: "region/alarm/daily/shot_bak",
                       data: "url="+imgbase64,
                       success: function(msg){
                         console.log(msg);
                       }
                    });
                  },
                });
            })
        },
        add: function () {
            $("#c-mail_group_ids").attr("readOnly","true");            
            $("#c-mail_ids").attr("readOnly","true");
            $("#c-wechat_department_ids").attr("readOnly","true");
            $("#c-wechat_user_ids").attr("readOnly","true");             
            //  选择不同的报警方式展示不同的列表
            var alarm_way_data = $("select[name='row[alarm_way]']").find('option:selected').val();
            $("select[name='row[alarm_way]']").change(function(){                
               var data_name = $("select[name='row[alarm_way]']").find('option:selected').val();
               console.log(data_name)
               if(data_name == '9'){
                    $("#mail").show();
                    $("#weach").show();            
                    $("#ding").show();
               }else if(data_name == '1'){
                    $("#mail").show();
                    $("#weach").hide();
                    $("#ding").hide();
               }else if(data_name == '2'){
                    $("#mail").hide();
                    $("#weach").show();
                    $("#ding").hide();
               }else if(data_name == '3'){
                    $("#mail").hide();
                    $("#weach").hide();
                    $("#ding").show();

               }
            })
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-mail_group_ids").attr("readOnly","true");            
            $("#c-mail_ids").attr("readOnly","true");
            $("#c-wechat_department_ids").attr("readOnly","true");
            $("#c-wechat_user_ids").attr("readOnly","true");   
            var alarm_way_data = $("select[name='row[alarm_way]']").find('option:selected').val();
            if(alarm_way_data == 9){
                $("#mail").show();
                $("#weach").show();
                $("#ding").show();
            }else if(alarm_way_data == 1){
                $("#mail").show();
                $("#weach").hide();
                $("#ding").hide();
            }else if(alarm_way_data == 2){
                $("#mail").hide();
                $("#weach").show();
                $("#ding").hide();
            }else if(alarm_way_data == 3){
                $("#mail").hide();
                $("#weach").hide();
                $("#ding").show();
            }
            //  选择不同的报警方式展示不同的列表
            $("select[name='row[alarm_way]']").change(function(){
               var data_name = $("select[name='row[alarm_way]']").find('option:selected').val();
               if(data_name == '9'){
                    $("#mail").show();
                    $("#weach").show();
                   $("#ding").show();
               }else if(data_name == '1'){
                    $("#mail").show();
                    $("#weach").hide();
                   $("#ding").hide();
               }else if(data_name == '2'){
                    $("#mail").hide();
                    $("#weach").show();
                   $("#ding").hide();
               }else if(data_name == '3'){
                    $("#mail").hide();
                    $("#weach").hide();
                   $("#ding").show();
               }
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});